package com.zzz.demo.controller;

import com.zzz.demo.event.ContentModifyEvent;
import com.zzz.demo.service.HelloService;
import com.zzz.demo.utils.ShellUtil;
import com.zzz.demo.utils.UpdateListGenerator;
import lombok.extern.slf4j.Slf4j;
import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

import static com.zzz.demo.utils.ShellUtil.COMMANDS;

/**
 * @author ZZZ
 * @version 1.0
 * @date 2018/12/3 003
 */
@RestController
@Slf4j
public class DemoController {

    @Autowired
    private HelloService helloService;
    @Autowired
    private ApplicationContext applicationContext;

    @GetMapping("test")
    public String testPrint() {
        System.out.println("hello world.....");
        return "Hello World";
    }

    @GetMapping("testHello")
    public List<String> testHello() {
        return helloService.testHello();
    }

    @GetMapping("getSysInfo")
    public String getSysInfo() {
        Map<String, String> resultMap = ShellUtil.runLocalShell(COMMANDS);
        String resultMessage = ShellUtil.disposeResultMessage(resultMap);
        log.info(resultMessage);
        return resultMessage;
    }

    @GetMapping("/cpuInfo")
    public CpuInfo[] getCpuInfo() throws SigarException {
        Sigar sigar = new Sigar();
        CpuInfo infos[] = sigar.getCpuInfoList();
        return infos;
    }

    @GetMapping("/testEvent")
    public String testEvent() {
        ContentModifyEvent modifyEvent = new ContentModifyEvent(applicationContext, "测试事件机制");
        applicationContext.publishEvent(modifyEvent);
        return modifyEvent.getMessage();
    }

    @PostMapping("/getFilePath")
    public String getFilePath(String projectName,String content) {
        return new UpdateListGenerator().getFilePath(projectName,content);
    }

}
